using Abc.Core.Entities;

using Furion.DatabaseAccessor;
using Furion.DependencyInjection;

using Mapster;

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

using System.Linq;
using System.Threading.Tasks;

using Tool;

namespace Abc.Application
{
    /// <summary>
    /// 记事本
    /// </summary>
    [ApiDescriptionSettings(Name = "Notepad")]
    //[Injection(Proxy = typeof(LogDispatchProxy))]
    [ServiceFilter(typeof(ApiActionFilterAttribute))]
    public class NotepadService : INotepadService, ITransient
    {
        private readonly ILogger<NotepadService> _logger;
        private readonly IRepository<Notepad> _notepadRepository;

        //private readonly IMemoryCache _memoryCache;
        public NotepadService(ILogger<NotepadService> logger
        //, IMemoryCache memoryCache
        , IRepository<Notepad> notepadRepository
         )
        {
            _logger = logger;
            // _memoryCache = memoryCache;
            //_memoryCache =  App.GetService<IMemoryCache>();
            _notepadRepository = notepadRepository;
        }

        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public async Task<ResultData<string>> InsertOrUpdate(NotepadInput input)
        {
            var resultdata = new ResultData<string>();
            if (input.Id > 0)
            {
                var dbentity = await _notepadRepository.FindAsync(input.Id);
                input.Adapt(dbentity);
                await _notepadRepository.UpdateAsync(dbentity);
            }
            else
            {
                var newEntity = await _notepadRepository.InsertAsync(input.Adapt<Notepad>());
            }
            resultdata.Succeeded = true;
            resultdata.Errors = "成功";
            return resultdata;
        }

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        public async Task<ResultData<string>> Delete(int id)
        {
            var resultdata = new ResultData<string>();
            await _notepadRepository.DeleteAsync(id);

            resultdata.Succeeded = true;
            resultdata.Errors = "成功";
            return resultdata;
        }

        /// <summary>
        /// 查询一条[跟踪实体]
        /// </summary>
        /// <param name="id"></param>
        public async Task<NotepadOut> Find(int id)
        {
            var dbentity = await _notepadRepository.FindAsync(id);
            return dbentity.Adapt<NotepadOut>();
        }

        /// <summary>
        /// 查询一条[跟踪实体]
        /// </summary>
        /// <param name="id"></param>
        public async Task<NotepadOut> Get(int id)
        {
            var dbentity = await _notepadRepository.FirstOrDefaultAsync(o => o.Id == id, tracking: false);
            if (dbentity == null) return null;
            return dbentity.Adapt<NotepadOut>();
        }

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="searchQuery"></param>
        /// <returns></returns>
        public async Task<PagedListToLayui<NotepadOut>> GetAllByPageToLayui(NotepadQuery searchQuery)
        {
            //var queryable= _notepadRepository.DetachedEntities.AsQueryable();
            //if (!string.IsNullOrWhiteSpace(searchQuery.KeyWords))
            //{
            //    var kw = searchQuery.KeyWords.ToLower();
            //    queryable = queryable.Where(o => o.No.Contains(kw));
            //}

            var pageResult = _notepadRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<NotepadOut>();
            return await pageResult.ToPagedListToLayuiAsync(searchQuery.Page, searchQuery.Limit);
        }

        #region 默认未启用

        ///// <summary>
        ///// 新增一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Insert(NotepadInput input)
        //{
        //    var resultdata = new ResultData<string>();
        //    var newEntity = await _notepadRepository.InsertAsync(input.Adapt<Notepad>());
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Update(int id,NotepadInput input)
        //{
        //    var resultdata=new ResultData<string>();
        //    var dbentity = await _notepadRepository.FindAsync(id);
        //    input.Adapt(dbentity);
        //    await _notepadRepository.UpdateAsync(dbentity);
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Inserts(List<NotepadInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    var dbaddinputs = inputs.Adapt<List<Notepad>>();
        //    await _notepadRepository.InsertAsync(dbaddinputs);
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> Updates(List<NotepadInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    var ids = inputs.Select(o => o.Id).ToList();
        //    var dbentitys = await _notepadRepository.Where(u => ids.Any(a => a == u.Id)).ToListAsync();
        //    inputs.Adapt(dbentitys);
        //    await _notepadRepository.UpdateAsync(dbentitys);
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> InsertOrUpdates(List<NotepadInput> inputs)
        //{
        //    var resultdata = new ResultData<string>();
        //    if (inputs.Any(o => o.Id > 0))
        //    {
        //        var updates = inputs.Where(o => o.Id > 0);
        //        var ids = updates.Select(o=>o.Id).ToList();
        //        var dbentity =  _notepadRepository.Where(o=> ids.Any(a=>a==o.Id));
        //        inputs.Adapt(dbentity);
        //        await  _notepadRepository.UpdateAsync(dbentity);
        //    }
        //    else
        //    {
        //       var adds = inputs.Where(o => o.Id <= 0);
        //       await _notepadRepository.InsertAsync(adds.Adapt<List<Notepad>>());
        //
        //    }
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}
        //
        //
        ///// <summary>
        ///// 查询所有
        ///// </summary>
        ///// <returns></returns>
        //public async Task<List<NotepadDto>> GetAll()
        //{
        //    var entities = _notepadRepository.DetachedEntities.AsQueryable().ProjectToType<NotepadDto>();
        //    var datas= await entities.ToListAsync();
        //    datas.SetMemberCache("Table_Notepad");
        //    return datas;
        //}
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        //public async Task<PagedList<NotepadDto>> GetAllByPage(NotepadQuery searchQuery)
        //{
        //    //var queryable= _notepadRepository.DetachedEntities.AsQueryable(tracking:false);
        //    //if (!string.IsNullOrWhiteSpace(searchQuery.KeyWords))
        //    //{
        //    //    var kw = searchQuery.KeyWords.ToLower();
        //    //    queryable = queryable.Where(o => o.No.Contains(kw));
        //    //}
        //    var pageResult = _notepadRepository.DetachedEntities.AsQueryable().OrderByDescending(o => o.Id).ProjectToType<NotepadDto>();
        //
        //    return await pageResult.ToPagedListAsync(searchQuery.Page, searchQuery.Limit);
        //}
        //
        //
        //
        //
        ///// <summary>
        ///// 设置状态
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="state"></param>
        ///// <param name="field">字段名</param>
        ///// <returns></returns>
        //public async Task<ResultData<string>> SetState(int id, bool state, string field)
        //{
        //    var resultdata = new ResultData<string>();
        //    var entity = await _notepadRepository.FirstOrDefaultAsync(o => o.Id == id);
        //
        //    switch (field.ToLower())
        //    {
        //
        //
        //
        //
        //
        //
        //
        //
        //
        //        default:
        //            break;
        //    }
        //
        //    resultdata.Succeeded = true;
        //    resultdata.Errors = "成功";
        //    return resultdata;
        //}

        #endregion 默认未启用
    }

    public interface INotepadService
    {
        /// <summary>
        /// 新增或更新一条
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        Task<ResultData<string>> InsertOrUpdate(NotepadInput input);

        /// <summary>
        /// 删除一条
        /// </summary>
        /// <param name="id"></param>
        Task<ResultData<string>> Delete(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<NotepadOut> Find(int id);

        /// <summary>
        /// 查询一条
        /// </summary>
        /// <param name="id"></param>
        Task<NotepadOut> Get(int id);

        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="searchQuery"></param>
        /// <returns></returns>
        Task<PagedListToLayui<NotepadOut>> GetAllByPageToLayui(NotepadQuery searchQuery);

        #region 默认未启用

        ///// <summary>
        ///// 新增一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Insert(NotepadInput input);
        //
        ///// <summary>
        ///// 更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Update(int id,NotepadInput input);
        ///// <summary>
        ///// 新增或更新一条
        ///// </summary>
        ///// <param name="input"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Inserts(List<NotepadInput> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> Updates(List<NotepadInput> inputs);
        ///// <summary>
        ///// 新增或更新多条
        ///// </summary>
        ///// <param name="inputs"></param>
        ///// <returns></returns>
        //Task<ResultData<string>> InsertOrUpdates(List<NotepadInput> inputs);
        ///// <summary>
        ///// 查询所有
        ///// </summary>
        ///// <returns></returns>
        //Task<List<NotepadDto>> GetAll();
        ///// <summary>
        ///// 分页查询
        ///// </summary>
        ///// <param name="searchQuery"></param>
        ///// <returns></returns>
        //Task<PagedList<NotepadDto>> GetAllByPage(NotepadQuery searchQuery);
        ///// <summary>
        ///// 设置状态
        ///// </summary>
        ///// <param name="id"></param>
        ///// <param name="state"></param>
        ///// <param name="field">字段名</param>
        ///// <returns></returns>
        //Task<ResultData<string>> SetState(int id, bool state, string field);

        #endregion 默认未启用
    }
}